package cn.itsource.pethome.system.dept.controller;

import cn.itsource.pethome.annotation.PreAuthorize;
import cn.itsource.pethome.system.dept.domain.Department;
import cn.itsource.pethome.system.dept.query.DepartmentQuery;
import cn.itsource.pethome.system.dept.service.IDepartmentService;
import cn.itsource.pethome.utils.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
/**
 * 他们都用来处理json的
 * @RequestBody 是前端传进来的
 * @ResponseBody 是后端返回给前端的
 */
public class DepartmentContrllor {
    @Autowired
    private IDepartmentService service;


    @PreAuthorize(name = "获取部门所有信息",value = "dept:all")
            @GetMapping("/list")
            @ApiOperation(value = "获取所有部门信息")
            public List<Department> findAll(){
                return service.findAll();
            }

/*            @GetMapping("/{id}")
            @PreAuthorize(name = "获取部门所有信息",value = "dept:all")*/
            @ApiOperation(value = "根据ID查询所有部门")
            public AjaxResult findOne(@PathVariable Long id){
                try {
                    Department findnoe = service.findnoe(id);
                    return AjaxResult.success().setData(findnoe);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return AjaxResult.error();
            }

            @PostMapping("/pagelist")
            @PreAuthorize(name = "获取部门分页信息",value = "dept:list")
            @ApiOperation(value = "分页")
            public AjaxResult findQuey(@RequestBody DepartmentQuery query){
                try {
                    List<Department> queryData = service.queryData(query);
                    Long count = service.queryCount(query);
                    return AjaxResult.success().setData(queryData).setTotal(count);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return AjaxResult.error();
            }

            @DeleteMapping("/{id}")
            @PreAuthorize(name = "删除部门信息",value = "dept:delete")
            @ApiOperation(value = "根据id删除部门")
            public AjaxResult delete(@PathVariable Long id){
                try {
                    service.delete(id);
                    return AjaxResult.success().setMsg("删除成功");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return AjaxResult.error().setMsg("删除失败");
            }

            @PutMapping
            @PreAuthorize(name = "保存部门信息",value = "dept:save")
            @ApiOperation(value = "保存部门信息【有id则为修改】")
            public AjaxResult seve(@RequestBody Department department){
                try {
                    if (department.getId() != null){
                        service.update(department);
                    }else {
                        service.add(department);
                    }
                    return AjaxResult.success();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return AjaxResult.error().setMsg("保存失败");
            }
    @PatchMapping("/del")
    @PreAuthorize(name = "批量删除部门",value = "dept:batchdel")
    @ApiOperation(value = "根据id批量删除部门")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        try {
            service.batchDel(ids);
            return AjaxResult.success().setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("删除失败");
    }

            @GetMapping("/tree")
            @ApiOperation(value = "返回部门树")
            public List<Department> getDeptTree(){
                return service.deptTree();
            }

}
